import Taro from '@tarojs/taro'

const env = 'dev'
let baseUrl = ''
switch (env) {
  case 'dev':
    baseUrl = 'https://测试'
    break
  case 'prod':
    baseUrl = 'https://生产'
    break
  default:
    break
}
export const wxRequest = data => {
  let header = {
    token: wx.getStorageSync('token')
  }
  return new Promise((resolve, reject) => {
    wx.request({
      url: baseUrl + data.url,
      method: data.method || 'POST',
      data: data.data || {},
      header: {
        'Content-Type': 'application/json',
        token: wx.getStorageSync('token')
      },
      success: res => {
        if (res.data.code === 'S0001') {
          wx.removeStorage({
            key: 'token'
          })
          wx.showToast({
            title: '登录超时，即将跳转登录页面',
            icon: 'none',
            duration: 1300,
            success: () => {
              wx.reLaunch({
                url: '/pages/login/index'
              })
              console.log('打印了')
            }
          })
        } else {
          resolve(res)
          console.log(res);
        }
      },
      fail: res => {
        if (res.errMsg.includes('timeout')) {
          wx.showToast({
            title: '网络超时',
            icon: 'none'
          })

        } else {
          wx.showToast({
            title: '网络错误',
            icon: 'none'
          })
        }

        reject(res)
      },
      complete: res => {
      }
    })
  })
}